﻿using AutoMapper;
using MediatR;
using MyMES.Domain.W.Liststructure;
using MyMES.Domain.zmc;
using MyMES.Error;
using MyMES.Infrastructure.Interface;
using MyMES.Read.API.Application.Commends.Incominginformations;
using MyMES.Read.API.Application.Commends.W.realize;

namespace MyMES.Read.API.Application.CommendHandlers.Incominginformations
{
    public class GetSupplierCommendHandlers : IRequestHandler<GetSupplierCommends, APIResult<List<Supplier>>>
    {

        private readonly IBaseRepository<Supplier> supplierRepository;
        private readonly IMapper mapper;

        public GetSupplierCommendHandlers(IBaseRepository<Supplier> supplierRepository, IMapper mapper)
        {
            this.supplierRepository = supplierRepository;
            this.mapper = mapper;
        }

        /// <summary>
        /// 获取供应商列表
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIResult<List<Supplier>>> Handle(GetSupplierCommends request, CancellationToken cancellationToken)
        {
            try
            {
                APIResult<List<Supplier>> result = new APIResult<List<Supplier>>();

                result.Code = APIEnum.查询成功;
                result.Msg = "查询成功";
                string sql = "SELECT * FROM supplier";
                var list = await supplierRepository.SelectAsync(sql);
                if (!string.IsNullOrEmpty(request.SupplierNumber))
                {
                    list = list.Where(x => x.SupplierNumber.Contains(request.SupplierNumber)).ToList();
                }
                if (!string.IsNullOrEmpty(request.SupplierName))
                {
                    list = list.Where(x => x.SupplierName.Contains(request.SupplierName)).ToList();
                }
                if (!string.IsNullOrEmpty(request.SupplierAbbreviation))
                {
                    list = list.Where(x => x.SupplierAbbreviation.Contains(request.SupplierAbbreviation)).ToList();
                }
                if (!string.IsNullOrEmpty(request.SupplierEnglishName))
                {
                    list = list.Where(x => x.SupplierEnglishName.Contains(request.SupplierEnglishName)).ToList();
                }
                if (request.IsEnable != null)
                {
                    var isenable = request.IsEnable == 1 ? true : false;
                    list = list.Where(x => x.IsEnable == isenable).ToList();
                }
                result.data = list;
                return result;

            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
